package com.wu.shop.module.shop.ui.fragment

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.databinding.ViewDataBinding
import com.wu.common.base.ui.fragment.MvpBaseFragment
import com.wu.shop.R
import com.wu.shop.databinding.FragmentShopCarBinding
import com.wu.shop.module.shop.frame.presenter.ShopCarPresenter
import com.wu.shop.module.shop.frame.view.ShopCarView
import com.wu.shop.observable.ShopCarBuyObservable
import com.wu.shop.observable.shopcar.ShopCarBuyInfo
import java.util.*
import kotlin.collections.ArrayList


/**
 * @author wkq
 *
 * @date 2021年07月06日 11:45
 *
 *@des
 *
 */

class ShopCarFragment : MvpBaseFragment<ShopCarView, ShopCarPresenter, FragmentShopCarBinding>(),
    Observer, View.OnClickListener {

    var buyIds = ArrayList<String>()
    var isShowDelete = false
    var isCheckedAll = false

    companion object {
        fun newInstance(): ShopCarFragment {

            var mFragment = ShopCarFragment()
            return mFragment
        }
    }


    override fun getLayoutId(): Int {
        return R.layout.fragment_shop_car
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        ShopCarBuyObservable.addObserver(this)
        mvpView.initView()
    }

    override fun onDestroy() {
        super.onDestroy()
        ShopCarBuyObservable.deleteObserver(this)
    }

    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.tv_edit -> {
                if (isShowDelete) {
                    binding.rlDelete.visibility = View.GONE
                    binding.llBuy.visibility = View.VISIBLE
                } else {
                    binding.rlDelete.visibility = View.VISIBLE
                    binding.llBuy.visibility = View.GONE
                }
                isShowDelete = !isShowDelete
            }
            R.id.bt_delete -> {mvpView.processDelete()}
            R.id.bt_buy -> {  mvpView.processSettlement()}
            R.id.cb_all -> {  mvpView.processCheck()}
        }
    }


    override fun update(o: Observable?, arg: Any?) {
        if (o is ShopCarBuyObservable) {
            var info = arg as ShopCarBuyInfo
            mvpView.addOrSub(info)
        }
    }

}